package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.EmploymentEntity;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.sgb.kjwl.model.entity.FindPeopleEntity;
import com.sgb.kjwl.model.entity.TeamPriceItemEntity;
import com.sgb.kjwl.model.entity.packers.EngineeringCompanyEntity;
import com.sgb.kjwl.model.entity.packers.LabourServiceAndEmploymentEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.MLog;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author : younghow
 * @date : 2021/7/12 17:18
 * description :智能关注的viewModel
 */
public class PackerAttentionDetailViewModel extends AndroidViewModel {

    public MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> successLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> errorLiveData = new MutableLiveData<>();
    public MutableLiveData<List<LabourServiceAndEmploymentEntity>> mFollowSuccessLiveData = new MutableLiveData<>();
    public MutableLiveData<List<EngineeringCompanyEntity>> mFollowCompanyLiveData = new MutableLiveData<>();
    public MutableLiveData<List<FindPackersEntity>> mFollowTeamLiveData = new MutableLiveData<>();
    public MutableLiveData<List<FindPeopleEntity>> mFollowWorkerLiveData = new MutableLiveData<>();
    public MutableLiveData<List<EmploymentEntity>> mFollowTemporaryEmploymentLiveData = new MutableLiveData<>();
    public MutableLiveData<List<TeamPriceItemEntity>> mFollowJobTypeQuotationLiveData = new MutableLiveData<>();
    public MutableLiveData<String> mUrlLiveData = new MutableLiveData<>();

    public PackerAttentionDetailViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void signUpTerm(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getPackerAttentionLaborList(map).enqueue(new Callback<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> call, Response<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollowSuccessLiveData.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<LabourServiceAndEmploymentEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public List<String> firstTitle(){
        List<String> list = new ArrayList<>();
        list.add("工程工队");
        list.add("找工人");
        list.add("临时用工");
        list.add("工种报价");
        list.add("劳务用工");
//        list.add("工程公司");
        return list;
    }

    public List<String> secondTitle(){
        List<String> list = new ArrayList<>();
        list.add("施工队");
        list.add("班组工队");
        return list;
    }

    public void getCompanyList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getPackerAttentionCompanyList(map).enqueue(new Callback<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>> call, Response<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollowCompanyLiveData.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getTeamList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getAttentionTeamList(map).enqueue(new Callback<BaseEntity<ListResponse<List<FindPackersEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<FindPackersEntity>>>> call, Response<BaseEntity<ListResponse<List<FindPackersEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollowTeamLiveData.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<FindPackersEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getWorkerList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getPackerAttentionWorkerList(map).enqueue(new Callback<BaseEntity<ListResponse<List<FindPeopleEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<FindPeopleEntity>>>> call, Response<BaseEntity<ListResponse<List<FindPeopleEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollowWorkerLiveData.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<FindPeopleEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getTemporaryEmploymentList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getTemporaryEmploymentList(map).enqueue(new Callback<BaseEntity<ListResponse<List<EmploymentEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EmploymentEntity>>>> call, Response<BaseEntity<ListResponse<List<EmploymentEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollowTemporaryEmploymentLiveData.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EmploymentEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getJobTypeQuotationList(Map<String,Object> map){
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getJobTypeQuotationList(map).enqueue(new Callback<BaseEntity<ListResponse<List<TeamPriceItemEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TeamPriceItemEntity>>>> call, Response<BaseEntity<ListResponse<List<TeamPriceItemEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mFollowJobTypeQuotationLiveData.postValue(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TeamPriceItemEntity>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    /**
     * 工队详情H5链接
     *
     * @param id 当前工队的no
     */
    public void getH5Url(String id) {
        loadingLiveData.postValue(true);
        APIManager.getInstance().getPickAPI().getUrlSetting("teamWorkDetail").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + id + "&type=0";
                    MLog.d("api", "H5跳转链接  " + url);
                    mUrlLiveData.postValue(url+"----"+id);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }
}
